#ifndef WIFI_MANAGER_H
#define WIFI_MANAGER_H

#include "led_manager.h"
#include "led_manager_adapter_espidf.h"
#include "esp_wifi.h"
#include "esp_http_server.h"
#include "cJSON.h"
#include <stdbool.h>
#include <stdint.h>
#include "nvs.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "esp_log.h"

#define MAX_RPC_METHODS 8

typedef char* (*RpcHandler)(cJSON* params);

typedef struct {
    const char* name;
    RpcHandler handler;
} RpcMethod;

typedef struct {
    gpio_num_t config_button_pin;
    bool in_config_mode;

    char wifi_ssid[64];
    char wifi_pass[64];
    char tb_host[64];
    char tb_token[128];
    uint32_t tb_port;

    unsigned long button_press_time;

    httpd_handle_t server;

    RpcMethod rpc_methods[MAX_RPC_METHODS];
    int rpc_method_count;
} WifiManager;
char* get_mqtt_uri();
char* get_mqtt_token();
void launch_wifi_ap_with_web_config(void);
void wifi_manager_init();
void wifi_manager_begin(void);
bool wifi_manager_is_config_mode(void);
bool wifi_manager_is_connected(void);
void wifi_manager_send_telemetry(const char* json_payload);
void wifi_manager_subscribe_rpc_method(const char* method, RpcHandler handler);

#endif // WIFI_MANAGER_H
